import { ref } from 'vue';
import { FormSchema } from '/@/components/Table';
import { BasicColumn } from '/@/components/Table';

const isKafka = (type) => type === 'KAFKA';
const isRocketMQ = (type) => type === 'ROCKETMQ';
const isRabbitMQ = (type) => type === 'RABBITMQ';
const isPulsar = (type) => type === 'PULSAR';
const isDefault = (type) => type !== 'NACOS';
const isNacos = (type) => type === 'NACOS';

export const extendFormFieldOptions = {
  KAFKA: [
    { label: 'auto.offset.reset', value: 'auto.offset.reset' },
    { label: 'max.poll.records', value: 'max.poll.records' },
  ],
  ROCKETMQ: [
    { lable: 'consumeFromWhere', value: 'consumeFromWhere'},
  ],
  RABBITMQ: [
    { lable: 'networkRecoveryInterval', value: 'networkRecoveryInterval'},
  ],
  PULSAR:[
    { lable: 'ioThreads', value: 'ioThreads'},
    { lable: 'subscriptionType', value: 'subscriptionType'},
    { lable: 'subscriptionName', value: 'subscriptionName'},
  ]
}

export const searchFormSchema: FormSchema[] = [
  {
    field: 'command',
    label: '数据源类型',
    component: 'Select',
    colProps: { span: 6 },
    componentProps: {
      options: [
        { label: 'Kafka', value: 'Kafka' },
        { label: 'RocketMQ', value: 'RocketMQ' },
        { label: 'RabbitMQ', value: 'RabbitMQ' },
      ],
    },
  },
  {
    field: 'commandName',
    label: '名称',
    colProps: { span: 6 },
    component: 'Input',
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'charset',
    label: '编码',
    component: 'JDictSelectTag',
    required: true,
    componentProps: {
      dictCode: 'character_encoding',
      placeholder: '请选择设备状态',
      stringToNumber: false,
    },
    defaultValue: 'ISO_8859_1',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'command',
    label: '数据源类型',
    component: 'Input',
    show: false,
  },
  {
    field: 'commandName',
    label: '指令消费名称',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
  },
  {
    label: '是否启用',
    field: 'status',
    component: 'JSwitch',
    colProps: { span: 12 },
    componentProps: {
      options: ['1', '0'],
    },
  },
  {
    field: 'poolSize',
    label: '线程数',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: '1',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'poolTimeOut',
    label: '轮询周期(毫秒)',
    colProps: { span: 12 },
    component: 'Input',
    required: true,
    defaultValue: '10',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'divider-properties',
    component: 'Divider',
    label: '数据源配置',
    componentProps: {
      plain: false,
    },
    ifShow: ({ values }) => isDefault(values.command),
  },
  {
    field: 'bootstrap.servers',
    label: 'bootstrap.servers',
    component: 'Input',
    required: true,
    defaultValue: '127.0.0.1:9092',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'group.id',
    label: 'group.id',
    component: 'Input',
    required: true,
    defaultValue: 'Fluxmq_consumer',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'key.deserializer',
    label: 'key.deserializer',
    component: 'Input',
    required: true,
    defaultValue: 'org.apache.kafka.common.serialization.StringDeserializer',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'value.deserializer',
    label: 'value.deserializer',
    component: 'Input',
    required: true,
    defaultValue: 'org.apache.kafka.common.serialization.StringDeserializer',
    ifShow: ({ values }) => isKafka(values.command),
  },
  {
    field: 'namesrvAddr',
    label: 'namesrvAddr',
    component: 'Input',
    required: true,
    defaultValue: '127.0.0.1:9786',
    ifShow: ({ values }) => isRocketMQ(values.command),
  },
  {
    field: 'consumerGroup',
    label: 'consumerGroup',
    component: 'Input',
    required: true,
    defaultValue: 'Fluxmq_consumer',
    ifShow: ({ values }) => isRocketMQ(values.command),
  },
  {
    field: 'host',
    label: 'host',
    component: 'Input',
    required: true,
    defaultValue: '127.0.0.1',
    ifShow: ({ values }) => isRabbitMQ(values.command),
  },
  {
    field: 'port',
    label: 'port',
    component: 'Input',
    required: true,
    defaultValue: '5672',
    ifShow: ({ values }) => isRabbitMQ(values.command),
  },
  {
    field: 'username',
    label: 'username',
    component: 'Input',
    required: true,
    defaultValue: 'guest',
    ifShow: ({ values }) => isRabbitMQ(values.command),
  },
  {
    field: 'password',
    label: 'password',
    component: 'Input',
    required: true,
    defaultValue: 'guest',
    ifShow: ({ values }) => isRabbitMQ(values.command),
  },
  {
    field: 'vhost',
    label: 'vhost',
    component: 'Input',
    required: true,
    defaultValue: '/',
    ifShow: ({ values }) => isRabbitMQ(values.command),
  },
  {
    field: 'serviceUrl',
    label: 'serviceUrl',
    component: 'Input',
    required: true,
    defaultValue: '127.0.0.1:6650',
    ifShow: ({ values }) => isPulsar(values.command),
  },
  {
    field: 'connectTimeout',
    label: 'connectTimeout',
    component: 'Input',
    required: true,
    defaultValue: '10',
    ifShow: ({ values }) => isPulsar(values.command),
  },
  {
    field: 'operationTimeout',
    label: 'operationTimeout',
    component: 'Input',
    required: true,
    defaultValue: '10',
    ifShow: ({ values }) => isPulsar(values.command),
  },
  {
    field: 'listenerThreads',
    label: 'listenerThreads',
    component: 'Input',
    required: true,
    defaultValue: '/',
    ifShow: ({ values }) => isPulsar(values.command),
  },

  {
    field: 'zcdz',
    label: '注册地址',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: '127.0.0.1:8848',
    ifShow: ({ values }) => isNacos(values.command),
  },
  {
    field: 'namespace',
    label: 'namespace',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: 'DEFAULT',
    ifShow: ({ values }) => isNacos(values.command),
  },
  {
    field: 'fwmc',
    label: '服务名称',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: 'fluxmq',
    ifShow: ({ values }) => isNacos(values.command),
  },
  {
    field: 'fz',
    label: '分组',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: 'DEFAULT_GROUP',
    ifShow: ({ values }) => isNacos(values.command),
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: 'nacos',
    ifShow: ({ values }) => isNacos(values.command),
  },
  {
    field: 'password',
    label: '密码',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    defaultValue: 'nacos',
    ifShow: ({ values }) => isNacos(values.command),
  },
  {
    field: 'addField',
    label: '',
    component: 'Input',
    slot: 'add',
    ifShow: ({ values }) => isDefault(values.command),
  },
  {
    field: 'divider-mapping',
    component: 'Divider',
    label: '主题映射',
    componentProps: {
      plain: false,
    },
    ifShow: ({ values }) => isDefault(values.command),
  },
  {
    field: 'ztys',
    label: '',
    component: 'Input',
    slot: 'addTopic',
    ifShow: ({ values }) => isDefault(values.command),
  },
  {
    field: 'mapping',
    label: '',
    component: 'Input',
    slot: 'table',
    ifShow: ({ values }) => isDefault(values.command),
  },
];

export const mappingFormSchema: FormSchema[] = [
  {
    field: 'topic',
    label: '订阅源主题',
    component: 'Input',
    required: true,
  },
  {
    field: 'mqttTopic',
    label: 'MQTT主题',
    component: 'Input',
    required: true,
  },
  {
    field: 'qos',
    label: 'Qos',
    component: 'Input',
  },
  {
    field: 'retain',
    label: 'Retain',
    component: 'JSwitch',
    componentProps: {
      options: ['1', '0'],
    },
  },
];

export const columns: BasicColumn[] = [
  {
    title: '源主题',
    dataIndex: 'topic',
  },
  {
    title: 'MQTT主题',
    dataIndex: 'mqttTopic',
  },
  {
    title: 'Qos',
    dataIndex: 'qos',
  },
  {
    title: 'Retain',
    dataIndex: 'retain',
    customRender:(value)=>{
      return value == '1' ? '是' : '否'
    },
  },
];

export interface Source {
  img?: string;
  name?: string;
  bkground?: string;
}

export const sources: Source[] = [
  {
    img: '',
    name: 'KAFKA',
    bkground: 'rgba(243, 244, 246, 0.5)',
  },
  {
    img: '',
    name: 'ROCKETMQ',
    bkground: 'rgb(254, 250, 223)',
  },
  {
    img: '',
    name: 'RABBITMQ',
    bkground: 'rgb(254, 249, 219)',
  },
  {
    img: '',
    name: 'PULSAR',
    bkground: 'rgba(38, 134, 255, 0.19)',
  },
  {
    img: '',
    name: 'NACOS',
    bkground: 'rgb(229, 250, 252)',
  },
];
